package com.ruoyi.material_receive.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material_receive.domain.MaterialReceive;
import com.ruoyi.material_receive.service.IMaterialReceiveService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物资领取Controller
 * 
 * @author ruoyi
 * @date 2025-08-22
 */
@RestController
@RequestMapping("/material_receive/material_receive")
public class MaterialReceiveController extends BaseController
{
    @Autowired
    private IMaterialReceiveService materialReceiveService;

    /**
     * 查询物资领取列表
     */
    @PreAuthorize("@ss.hasPermi('material_receive:material_receive:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialReceive materialReceive)
    {
        startPage();
        List<MaterialReceive> list = materialReceiveService.selectMaterialReceiveList(materialReceive);
        return getDataTable(list);
    }

    /**
     * 导出物资领取列表
     */
    @PreAuthorize("@ss.hasPermi('material_receive:material_receive:export')")
    @Log(title = "物资领取", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialReceive materialReceive)
    {
        List<MaterialReceive> list = materialReceiveService.selectMaterialReceiveList(materialReceive);
        ExcelUtil<MaterialReceive> util = new ExcelUtil<MaterialReceive>(MaterialReceive.class);
        util.exportExcel(response, list, "物资领取数据");
    }

    /**
     * 获取物资领取详细信息
     */
    @PreAuthorize("@ss.hasPermi('material_receive:material_receive:query')")
    @GetMapping(value = "/{receiveId}")
    public AjaxResult getInfo(@PathVariable("receiveId") Long receiveId)
    {
        return success(materialReceiveService.selectMaterialReceiveByReceiveId(receiveId));
    }

    /**
     * 新增物资领取
     */
    @PreAuthorize("@ss.hasPermi('material_receive:material_receive:add')")
    @Log(title = "物资领取", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialReceive materialReceive)
    {
        // 1. 执行插入操作，MyBatis/MyBatis-Plus 会自动回填自增主键等信息到 materialReceive 对象
        int result = materialReceiveService.insertMaterialReceive(materialReceive);

        // 2. 从对象中获取所需参数
        Long receiveId = materialReceive.getReceiveId();// 自增主键（物资领取记录 ID）
        Long warehouseId = materialReceive.getWarehouseId();// 物资仓库 ID
        Long materialId = materialReceive.getMaterialId();// 物资 ID
        String receiveDept = materialReceive.getReceiveDept();// 领用部门
        Long quantity = materialReceive.getQuantity();// 领用数量
        String remark = materialReceive.getRemark();// 备注（假设实体类中有对应的 getRemark 方法，属性名为 remark ，按实际调整）
        Date receiveTime = materialReceive.getReceiveTime();// 领取时间

        // 3. 打印参数值
        System.out.println("===== 物资领取参数信息 =====");
        System.out.println("领取记录 ID（自增主键）: " + receiveId);
        System.out.println("物资仓库 ID: " + warehouseId);
        System.out.println("物资 ID: " + materialId);
        System.out.println("领用部门: " + receiveDept);
        System.out.println("领用数量: " + quantity);
        System.out.println("备注: " + (remark != null ? remark : "无"));
        System.out.println("领取时间: " + receiveTime);
        System.out.println("==========================");
        return toAjax(result);
    }

    /**
     * 修改物资领取
     */
    @PreAuthorize("@ss.hasPermi('material_receive:material_receive:edit')")
    @Log(title = "物资领取", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialReceive materialReceive)
    {
        return toAjax(materialReceiveService.updateMaterialReceive(materialReceive));
    }

    /**
     * 删除物资领取
     */
    @PreAuthorize("@ss.hasPermi('material_receive:material_receive:remove')")
    @Log(title = "物资领取", businessType = BusinessType.DELETE)
	@DeleteMapping("/{receiveIds}")
    public AjaxResult remove(@PathVariable Long[] receiveIds)
    {
        return toAjax(materialReceiveService.deleteMaterialReceiveByReceiveIds(receiveIds));
    }
}
